layui.use(["laytp"], function () {
    const funController = {};
    //静态页面地址前缀
    window.htmlPrefix = facade.compatibleHtmlPath("/admin/lol/hero/");
    //后端接口地址前缀
    window.apiPrefix  = facade.compatibleApiRoute("/admin.lol.hero/");
    //表格渲染
    funController.tableRender = function (where, page) {
        if(typeof where =="undefined"){
            where = {
                order_param :{
                    'type' :'desc',
                    'field' :'list_sort',
                }
            }
        }
        layui.table.render({
            elem: "#laytp-table"
            , id: "laytp-table"
            , url: window.apiPrefix+"index"
            , toolbar: "#default-toolbar"
            , defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
            , where: where
            , method: "GET"
            , cellMinWidth: 120
            , skin: 'line'
            , loading: false
            , page: {
                curr: page
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return facade.parseTableData(res, true);
            }
            , done: function(){
                layui.laytpTable.done();
            }
            , cols: [[
                {type:'checkbox',fixed:'left'}
                , {field:'id',title:'ID',align:'center',fixed:'left'}
                , {field: "list_sort", title: "排序", minWidth: 80, align: "center",fixed:'left', templet:function(d){
                        return laytpForm.tableForm.editInput('list_sort',d, window.apiPrefix+'setSort');
                    }}
                , {field:'name',title:'名称',align:'center',minWidth:120,fixed:'left'}
                , {field:'nickname',title:'称号',align:'center',minWidth:120,fixed:'left'}
                , {field:'type',title:'职责',align:'center',minWidth:80,fixed:'left'}
                , {field:'difficulty',title:'难度',align:'center',minWidth:80,fixed:'left'}
                , {
                    field: "url", title: "半身像", align: "center",fixed:'left',templet: function (d) {
                        return laytp.tableFormatter.images(d.url);
                    }
                }
                , {
                    field: "icon", title: "小头像", align: "center",fixed:'left',templet: function (d) {
                        return laytp.tableFormatter.images(d.icon);
                    }
                }
                , {
                    field: "status", title: "显示", align: "center", width: 90, templet: function (d) {
                        return laytpForm.tableForm.switch("is_show", d, {
                            "open": {"value": "yes", "text": "是"},
                            "close": {"value": "no", "text": "否"}
                        }, 'laytp-table-switch',true);
                    }
                }
                ,{field:'create_time',title:'创建时间',minWidth:178,align:'center'}
                ,{
                    field: "operation",
                    title: "操作",
                    toolbar: "#default-bar",
                    fixed: "right",
                    align: "center",
                    minWidth: 200
                }

            ]]
        });

        //监听数据表格顶部左侧按钮点击事件
        layui.table.on("toolbar(laytp-table)", function (obj) {
            //默认按钮点击事件，包括添加按钮和回收站按钮
            var defaultTableToolbar = layui.context.get("defaultTableToolbar");
            if (defaultTableToolbar.indexOf(obj.event) !== -1) {
                laytp.tableToolbar(obj);
            } else {
                // //自定义按钮点击事件
                // switch(obj.event){
                // //自定义按钮点击事件
                // case "":
                //
                //     break;
                // }
            }
        });

        //监听数据表格[操作列]按钮点击事件
        layui.table.on("tool(laytp-table)", function (obj) {
            console.log(obj)
            var defaultTableTool = layui.context.get("defaultTableTool");
            if (defaultTableTool.indexOf(obj.event) !== -1) {
                laytp.tableTool(obj);
            } else {
                // //自定义按钮点击事件
                switch(obj.event){
                //自定义按钮点击事件
                case "skin":
                    let options = {};
                    options.title = "皮肤管理";
                    options.title = "皮肤管理";
                    options.shade = 0.5;
                    options.path = facade.compatibleHtmlPath(window.htmlPrefix) + "skin.html?hero_id=" + obj.data.id;
                    facade.popupDiv(options);
                    break;
                }
            }
        });

        //监听表头排序事件
        layui.table.on('sort(laytp-table)', function(obj){
            layui.table.reload('laytp-table', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                , where: {
                    "order_param" : {
                        "field" : obj.field,
                        "type" : obj.type
                    }
                }
            });
        });
    };

    funController.tableRender();

    window.funController = funController;
});